package com.sirui.jpa.repository;

import com.sirui.jpa.model.JpaUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据操作层
 * 创建者
 * 创建时间	2017年7月25日
 */
public interface UserRepository extends JpaRepository<JpaUser, String>, JpaSpecificationExecutor<JpaUser> {

    JpaUser findByName(String name);

    JpaUser findByAge(Integer age);

    JpaUser findByNameAndAge(String name, Integer age);

    List<JpaUser> findByNameLike(String name);

    @Query("from JpaUser u where u.name=:name")
    JpaUser findUser(@Param("name") String name);

    boolean existsByAgeEquals(Integer age);

    @Transactional
    @Modifying
    @Query("update JpaUser j set j.name = ?1 where j.age = ?2")
    int updateNameByAgeEquals(String name, Integer age);

    @Transactional
    @Modifying
    @Query(value = "update t_jpa_user set name=?1 where age=?2", nativeQuery = true)
    int updateNameByAgeEqualsNative(String name, Integer age);

}
